﻿using System;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;
using BdcAdmin = Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.Office.Server.Utilities;
using Microsoft.BusinessData;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.MetadataModel;

namespace InstanceSiteVSTemplateWizard
{
    public partial class EntityDetailsInputForm : Form
    {

        public string entityselectedfieldsproperty;
        public string entitydeselectedfieldsproperty;
        public string entityfieldsproperty1, entityfieldsproperty2, entityfieldsproperty3, entityfieldsproperty4;

        public static string spportalName = Program.URL;

        public EntityDetailsInputForm()
        {
            InitializeComponent();
        }

        public EntityDetailsInputForm(NameValueCollection nm)
        {
            InitializeComponent();

            
            CheckBox[] cb1= new CheckBox[nm.Count];
            for ( int i=0; i <nm.Count; i++)
            {
                cb1[i] = new CheckBox();
                cb1[i].Text = nm.Keys[i];
                checkedListBox1.Items.Add(cb1[i].Text);
                
            }


        }
        
        // this method will be called when ISV clicks on Next button
        private void nextButton1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(str);
            generateEntityProperties();
            this.Close();
            Program.inputForm[Program.j].Close();

        }


        public void generateEntityProperties()
        {
            entitydeselectedfieldsproperty = generateDeSelectedEntityfieldsProperty();
            entityselectedfieldsproperty = generateSelectedEntityfieldsProperty();

            entityfieldsproperty1 = generateEntityfieldsProperty1();
            entityfieldsproperty2 = generateEntityfieldsProperty2();
            entityfieldsproperty3 = generateEntityfieldsProperty3();
            entityfieldsproperty4 = generateEntityfieldsProperty4(); ;
        }

        // this is for the de selected fields
        private string generateDeSelectedEntityfieldsProperty()
        {
            string str1 = null;                       
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemCheckState(i) != CheckState.Checked)
                {
                    str1 += "        &lt;string&gt;" + checkedListBox1.Items[i].ToString() + "&lt;/string&gt;";
                    str1 += System.Environment.NewLine;
                }
            }
            return str1;
        }

        // this is for the  selected fields
        private string generateSelectedEntityfieldsProperty()
        {
            string str2=null;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {               
                str2 += "        &lt;string&gt;" + checkedListBox1.CheckedItems[i].ToString() + "&lt;/string&gt;";
                str2 += System.Environment.NewLine;              
            }
            return str2;
        }


        // this is for the  generating entity properties1             
        private string generateEntityfieldsProperty1()
        {
            string str3=null;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                str3 += "           &lt;xsl:param name=" + '"' + "ColName_" + i + '"' + "&gt;" + checkedListBox1.CheckedItems[i].ToString() + "&lt;/xsl:param&gt;";
                str3 += System.Environment.NewLine;            
            }
            return str3;
        }

        // this is for the  generating entity properties2
        private string generateEntityfieldsProperty2()
        {
            string str4 = null;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                str4 += "          &lt;tr&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;td class=" + '"' + "ms-descriptiontext ms-alignright" + '"' + "&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;nobr&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:value-of select=" + '"' + "$ColName_" + i + '"' + " /&gt;:&lt;/nobr&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/td&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;td class=" + '"' + "ms-descriptiontext ms-alignleft" + '"' + " width=" + '"' + "100 %" + '"' + "&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:variable name=" + '"' + "fieldValue" + '"' + "&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:call-template name=" + '"' + "LFtoBR" + '"' + "&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:with-param name=" + '"' + "input" + '"' + "&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:value-of select=" + '"' + "@" + checkedListBox1.CheckedItems[i].ToString() + '"' + " /&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/xsl:with-param&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/xsl:call-template&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/xsl:variable&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;xsl:copy-of select=" + '"' + "$fieldValue" + '"' + " /&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/td&gt;";
                str4 += System.Environment.NewLine;
                str4 += "          &lt;/tr&gt;";
                str4 += System.Environment.NewLine;
            }
            return str4;
        }

        // this is for the  generating entity properties3
        private string generateEntityfieldsProperty3()
        {
            string str5 = null;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                str5 += " &lt;th class=" + '"' + "ms-vh" + '"' + " align=" + '"' + "left" + '"' + "&gt;";
                str5 += System.Environment.NewLine;
                str5 += "         &lt;xsl:call-template name=" + '"' + "dvt.headerfield" + '"' + " ddwrt:atomic=" + '"' + "1" + '"' + "&gt;";
                str5 += System.Environment.NewLine;
                str5 += "            &lt;xsl:with-param name=" + '"' + "fieldname" + '"' + "&gt;@" + checkedListBox1.CheckedItems[i].ToString() + "&lt;/xsl:with-param&gt;";
                str5 += System.Environment.NewLine;
                str5 += "           &lt;xsl:with-param name=" + '"' + "fieldtitle" + '"' + "&gt;";
                str5 += System.Environment.NewLine;
                str5 += "             &lt;xsl:value-of select=" + '"' + "$ColName_" + i + '"' + " /&gt;";
                str5 += System.Environment.NewLine;
                str5 += "           &lt;/xsl:with-param&gt;";
                str5 += System.Environment.NewLine;
                str5 += "           &lt;xsl:with-param name=" + '"' + "displayname" + '"' + "&gt;";
                str5 += System.Environment.NewLine;
                str5 += "             &lt;xsl:value-of select=" + '"' + "$ColName_" + i + '"' + " /&gt;";
                str5 += System.Environment.NewLine;
                str5 += "           &lt;/xsl:with-param&gt;";
                str5 += System.Environment.NewLine;
                str5 += "           &lt;xsl:with-param name=" + '"' + "fieldtype" + '"' + "&gt;text&lt;/xsl:with-param&gt;";
                str5 += System.Environment.NewLine;
                str5 += "          &lt;/xsl:call-template&gt;";
                str5 += System.Environment.NewLine;
                str5 += "        &lt;/th&gt;";
                str5 += System.Environment.NewLine;
            }
            return str5;
        }

        // this is for the  generating entity properties4
        private string generateEntityfieldsProperty4()
        {
            string str6 = null;
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                str6 += "&lt;td class=" + '"' + "ms-vb" + '"' + "&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:attribute name=" + '"' + "style" + '"' + "&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:choose&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:when test=" + '"' + "$dvt_1_form_selectkey = @*[name()=$ColumnKey]" + '"' + "&gt;color:blue&lt;/xsl:when&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:otherwise /&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/xsl:choose&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/xsl:attribute&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:variable name=" + '"' + "fieldValue" + '"' + "&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:call-template name=" + '"' + "LFtoBR" + '"' + "&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:with-param name=" + '"' + "input" + '"' + "&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:value-of select=" + '"' + "@" + checkedListBox1.CheckedItems[i].ToString() + '"' + " /&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/xsl:with-param&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/xsl:call-template&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/xsl:variable&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;xsl:copy-of select=" + '"' + "$fieldValue" + '"' + " /&gt;";
                str6 += System.Environment.NewLine;
                str6 += "&lt;/td&gt;    ";
                str6 += System.Environment.NewLine;
            }
            return str6;
        }

        // ISV clicks on Hider button
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.inputForm[Program.j].Show();
        }
        

 }
}
